package org.xenon.vote.service;

import org.springframework.stereotype.Service;
import org.xenon.vote.dao.imp.UserDao;
import org.xenon.vote.exception.UserExistException;
import org.xenon.vote.model.pojo.po.User;

@Service
public class UserService {
    private final UserDao userDao;

    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * 判断是否未注册
     */
    public boolean testUSer(String mail){
        return userDao.get(mail)==null;
    }

    public void addUser(String name, String passwd, String mail) throws UserExistException {
        if(!testUSer(mail)) throw new UserExistException(UserExistException.dftMsg);
        userDao.add(new User(name, passwd, mail, "user"));
    }

    public User login(String mail, String passwd) {
        User user = userDao.get(mail);
        if (user == null || !user.getPassword().equals(passwd)) return null;
        return user;
    }

    /**
     * 代理
     */
    public User getUSer(String mail){
        return userDao.get(mail);
    }
}
